package com.jzdjt.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.jzdjt.entity.ChargeEntity;
import com.jzdjt.tool.DBUtil;

public class OrderDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrderDetailServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		String orderID = request.getParameter("orderID");
		System.out.println("orderID:" + orderID);
		String result = getOrderDetail(orderID);
		PrintWriter out = response.getWriter();
		out.println(result);
		out.flush();
		out.close();
	}

	private String getOrderDetail(String orderID) {
		// TODO Auto-generated method stub
		JSONObject jsonObject = null;
		String getOrderDetailSql = "select * from recharge where OrderID='" + orderID + "'";
		DBUtil dbUtil = new DBUtil();
		List<ChargeEntity> orderlist = dbUtil.queryFromCharge(getOrderDetailSql);
		if (orderlist != null && orderlist.size() == 1) {
			ChargeEntity order = orderlist.get(0);
			HashMap<String, String> map = new HashMap<>();
			map.put("orderID", order.getOrderID());
			map.put("name", order.getName());
			map.put("orderTime", order.getFeeData());
			map.put("feeAmount", "￥" + order.getFeeAmount());
			String object = new Gson().toJson(map);
			try {
				jsonObject = new JSONObject(object);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return jsonObject.toString();
	}

}
